/*
 *   LG ELECTRONICS INC., SEOUL, KOREA
 *   Copyright(c) 2011 by LG Electronics Inc.
 *
 *   All rights reserved. No part of this work may be reproduced, stored in a
 *   retrieval system, or transmitted by any means without prior written
 *   Permission of LG Electronics Inc.
 */
package com.lge.clock.sui;

import java.lang.Character.UnicodeBlock;

import android.content.Context;

import com.lge.sui.widget.R;
import com.lge.sui.widget.list2.SUIListResource;
import com.lge.sui.widget.list2.locale.SUILocaleUtil;

/**
 * 일본어 처리를 위한 SUILocaleUtil 클래스
 * 
 * @author kijoong.lee
 * @since SUI3.0
 */
public class JapaneseLocaleUtil extends SUILocaleUtil {

    //@Override
    public String getSectionIndex(String word) {          
        String firstLetter = null;
        if (word.length() > 0) {
            int codePoint = word.codePointAt(0);
            UnicodeBlock unicodeBlock = UnicodeBlock.of(codePoint);
            if (isNumber(codePoint)) {
                firstLetter = "#";
            } else if (isJapaneseHiraganaUnicodeBlock(unicodeBlock)) {
                firstLetter = getSectionIndexForJapaneseHiragana(codePoint);
            } else if (isJapaneseKatakanaUnicodeBlock(unicodeBlock)) {
                firstLetter = getSectionIndexForJapaneseKatakana(codePoint);     
            } else if (isLatinUnicodeBlock(unicodeBlock)) {
                firstLetter = word.substring(0, 1);                
            }
        } 
        
        if (firstLetter == null) {
            firstLetter = "他";
        }
                
        return firstLetter;
    }
    
    private static String getSectionIndexForJapaneseHiragana(int codePoint) {
        if (codePoint > "\u3040".codePointAt(0) && codePoint < "\u304B".codePointAt(0) ||
                codePoint == "\u3094".codePointAt(0) ) { // "ゔ"는 "あ"행으로 분류
            return "\u3042"; // a
        } else if (codePoint < "\u3055".codePointAt(0)) {
            return "\u304B"; // ka
        } else if (codePoint < "\u305F".codePointAt(0)) {
            return "\u3055"; // sa
        } else if (codePoint < "\u306A".codePointAt(0)) {
            return "\u305F"; // ta
        } else if (codePoint < "\u306F".codePointAt(0)) {
            return "\u306A"; // na
        } else if (codePoint < "\u307E".codePointAt(0)) {
            return "\u306F"; // ha
        } else if (codePoint < "\u3083".codePointAt(0)) {
            return "\u307E"; // ma
        } else if (codePoint < "\u3089".codePointAt(0)) {
            return "\u3083"; // ya
        } else if (codePoint < "\u308E".codePointAt(0)) {
            return "\u3089"; // ra
        } else if (codePoint <= "\u3093".codePointAt(0)) { // "ん"는 "わ"행으로 분류
            return "\u308E"; // wa
        } else if (codePoint < "\u3099".codePointAt(0)) {
            return "\u3096";
        }
        
        return null;
    }
    
    // 참조 : http://jrgraphix.net/r/Unicode/30A0-30FF(Katakana)
    private static String getSectionIndexForJapaneseKatakana(int codePoint) {
        if (codePoint > "\u30A0".codePointAt(0) && codePoint < "\u30AB".codePointAt(0) || 
                codePoint == "\u30F4".codePointAt(0)) { //"ヴ"는  "あ"행으로 분류  
            return "\u3042"; // a
        } else if (codePoint < "\u30B5".codePointAt(0)) {
            return "\u304B"; // ka
        } else if (codePoint < "\u30BF".codePointAt(0)) {
            return "\u3055"; // sa
        } else if (codePoint < "\u30CA".codePointAt(0)) {
            return "\u305F"; // ta
        } else if (codePoint < "\u30CF".codePointAt(0)) {
            return "\u306A"; // na
        } else if (codePoint < "\u30DE".codePointAt(0)) {
            return "\u306F"; // ha
        } else if (codePoint < "\u30E3".codePointAt(0)) {
            return "\u307E"; // ma
        } else if (codePoint < "\u30E9".codePointAt(0)) {
            return "\u3083"; // ya
        } else if (codePoint < "\u30EE".codePointAt(0)) {
            return "\u3089"; // ra
        } else if (codePoint <= "\u30F3".codePointAt(0)) { // "ン"는 "わ"행으로 분류
            return "\u308E"; // wa
        } else if (codePoint < "\u30F9".codePointAt(0)) {
            return "\u3096";
        }
        
        return null;
    }
    
    private static boolean isJapaneseHiraganaUnicodeBlock(UnicodeBlock unicodeBlock) {
        return unicodeBlock == UnicodeBlock.HIRAGANA;
    }
    
    private static boolean isJapaneseKatakanaUnicodeBlock(UnicodeBlock unicodeBlock) {
        return unicodeBlock == UnicodeBlock.KATAKANA ||
                unicodeBlock == UnicodeBlock.KATAKANA_PHONETIC_EXTENSIONS;
    }
    
    public int getMatchedSection(String scrollerChar) {
    	return 0;
    }
    
    //@Override
    protected CharSequence getAlphabetChars(Context context) {
        return (context == null) ? null : 
            context.getString(R.string.custom_japanese_alphabet);
    }
    
    //@Override
    protected CharSequence getSectionChars(Context context) {
        return (context == null) ? null : 
            context.getString(R.string.custom_japanese_sections);
    }
}